<template>
  <div class="dashboard-container">
    <h1 style="text-align: center">黑名单</h1>

    <el-dialog title="删除" :visible.sync="deleteButtonController" width="30%" @close="closeDelete('ruleForm')">
      <div style="margin: 10px"><span>请确认是否拉出黑名单</span></div>
      <span slot="footer" class="dialog-footer">
          <el-button type="info" plain @click="deleteBlackList('ruleForm')">确定</el-button>
          <el-button plain @click="closeDelete('ruleForm')">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :visible.sync="updateButtonController"
      title="修改"
      width="50%"
      @close="closeBlackList('ruleForm')"
    >
      <el-form label-width="20%" :model="updateList" :rules="BlackLists" ref="ruleForm">
        <el-form-item label="失信程度" prop="state">
          <el-select v-model="updateList.state" clearable placeholder="失信程度" style="width: 80%" >
            <el-option
              v-for="item in states"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信誉积分" prop="reputation">
          <el-input v-model="updateList.reputation"  maxlength="18" show-word-limit style="width: 80%" clearable
                    placeholder="请输入信誉积分"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="updateList.remark" maxlength="100" show-word-limit style="width: 80%" clearable
                    placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="updateBlackList('ruleForm')">确 定</el-button>
    <el-button @click="closeBlackList('ruleForm')">取 消</el-button>

  </span>
    </el-dialog>


    <el-input
      v-model="blackListId"
      placeholder="黑名单ID"
      name="blackListId"
      type="text"
      clearable
      style="margin-left:17% ;width: 180px"
    />
    <el-input
      v-model="customerId"
      placeholder="顾客ID"
      name="customerId"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <el-select
      v-model="state"
      clearable placeholder="失信程度"
      style="margin-left:40px ;width: 180px">
      <el-option
        v-for="item in states"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>

    <el-button plain
               style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: #cfefc6; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
               @click="selectAll"
    >
      查询
    </el-button>
    <el-button
      style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: white; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
      @click="checkBlackList"
    >重置
    </el-button>
    <el-table :data="blackList.slice((currentPage-1)*pagesize,currentPage*pagesize)" height="630px" border style="width: 100%;margin-top: 20px"
              class="elegant-room-table"
    >
      <el-table-column align="center" fixed prop="blackListId" label="黑名单ID" min-width="100"/>
      <el-table-column align="center" fixed prop="customerId" label="顾客ID" width="160"/>
      <el-table-column align="center" fixed prop="customerName" label="顾客名字" min-width="120"/>
      <el-table-column align="center" fixed prop="state" label="失信程度" min-width="100"/>
      <el-table-column align="center" fixed prop="dataUpdateTime" label="数据更新时间" width="190"/>
      <el-table-column align="center" fixed prop="debtMoney" label="欠款金额" min-width="120"/>
      <el-table-column align="center" fixed prop="debtDay" label="欠款天数" width="120"/>
      <el-table-column align="center" fixed prop="reputation" label="信誉积分" width="130"/>
      <el-table-column align="center" fixed prop="remark" label="备注" width="240"/>
      <el-table-column align="center" fixed label="操作" width="160">
        <template v-slot="scope">
          <el-button size="mini" round icon="el-icon-edit" @click="updateButton(scope.row)"></el-button>
          <el-button size="mini" round icon="el-icon-delete" @click="deleteButton(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top:5px;margin-left: 30%;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,8,10,12,15,20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="blackList.length">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteRoomType, insert1RoomType, selectAllRoomType, updateRoomType } from '@/api/roomType'
import { insertCustomer, selectAllCustomer, updateCustomer } from '@/api/customer'
import { selectAllLiveRecords } from '@/api/liveRecords'
import { abrogateStay1, cancelStay, insertStay, selectAllStay1 } from '@/api/stay'
import { collectionLive1, selectAllLive, settlementLive1 } from '@/api/live'
import { selectAllSystemLog } from '@/api/systemLog'
import { deleteBlackList1, selectAllBlackList, updateBlackList } from '@/api/blackList'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data() {
    return {
      blackListId: '',
      customerId: '',
      state:'',
      blackList: [],
      states:[
        {
          value: '轻度失信',
          label: '轻度失信'
        },
        {
          value: '中度失信',
          label: '中度失信'
        },
        {
          value: '重度失信',
          label: '重度失信'
        }
      ],
      updateList: {
        blackListTableId:'',
        blackListId:'',
        customerId:'',
        customerName:'',
        state:'',
        remark:'',
        dataUpdateTime:'',
        debtMoney:'',
        debtDay:'',
        reputation:''
      },
      BlackLists: {
        customerName: [
          {required: true, trigger: 'blur', message: '顾客名称不能为空'},
          {min: 2, max: 50, message: '请输入顾客名称', trigger: 'blur'}
        ],
        reputation: [
          { required: true, message: '请输入信誉积分', trigger: 'blur' },
          { validator: this.validateBlackListReputation, trigger: 'blur' }
        ],
        state:[
          {required: true, message: '请选择失信程度',trigger: 'change'},
        ]
      },
      updateButtonController:false,
      deleteButtonController:false,
      deleteBlackListTableId:'',
      deleteBlackListId:'',

      currentPage: 1,
      currentIndex: '',
      pagesize: 10,
    }
  },
  mounted() {
    this.selectAll()
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage= val
    },
    validateBlackListReputation(rule, value, callback) {
      if (!value || isNaN(value)) {
        callback(new Error('信誉积分必须为数字'))
      } else if (value < 0 || value > 100) {
        callback(new Error('信誉积分必须在0到100分之间'))
      } else {
        callback()
      }
    },
    selectAll() {
      selectAllBlackList({
        blackListId: this.blackListId,
        customerId: this.customerId,
        state:this.state,
      }).then(
        response => {
          console.log(response.data)
          this.blackList = response.data
        }
      )
    },
    updateBlackList(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateBlackList({
            blackListTableId:this.updateList.blackListTableId,
            customerId: this.updateList.customerId,
            customerName: this.updateList.customerName,
            blackListId:this.updateList.blackListId,
            state:this.updateList.state,
            remark:this.updateList.remark,
            dataUpdateTime:this.updateList.dataUpdateTime,
            debtMoney:this.updateList.debtMoney,
            debtDay:this.updateList.debtDay,
            reputation:this.updateList.reputation
          }).then(
            response => {
              this.updateButtonController = false
              this.$message.success('修改成功');
              this.selectAll()
            }
          )

        } else {
          return false
        }
      })
    },
    updateButton(row) {
      this.updateList = {
        blackListTableId: row.blackListTableId,
        customerId: row.customerId,
        customerName: row.customerName,
        blackListId:row.blackListId,
        state:row.state,
        dataUpdateTime:row.dataUpdateTime,
        debtMoney:row.debtMoney,
        debtDay:row.debtDay,
        reputation:row.reputation,
        remark: row.remark
      }
      this.updateButtonController = true
    },
    deleteBlackList(formName) {
      deleteBlackList1({
        blackListTableId:this.deleteBlackListTableId,
        blackListId:this.deleteBlackListId,
      }).then(
        async response => {
          this.deleteButtonController = false
          this.$message.success('已成功拉出黑名单');
          this.selectAll()
        })
    },
    deleteButton(row){
      this.deleteBlackListTableId = row.blackListTableId
      this.deleteBlackListId = row.blackListId
      this.deleteButtonController = true
    },
    checkBlackList(){
      this.blackListId= ''
      this.customerId= ''
      this.state= ''
      this.selectAll()
    },
    closeBlackList(formName) {
      this.$refs[formName].resetFields()
      this.updateButtonController = false
    },
    closeDelete(){
      this.deleteButtonController = false
    },
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.elegant-room-table {
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  overflow: hidden;

  &::before {
    display: none;
  }

  .price-tag {
    display: inline-block;
    padding: 3px 8px;
    color: #171717;
    border-radius: 12px;
    font-weight: 500;
  }
}

.status-green {
  color: green;
}

.status-red {
  color: red;
}
</style>
